package edu.ynu.e_platform.controllers;

import edu.ynu.e_platform.common.controller.LogicController;
import edu.ynu.e_platform.dao.BusinessDao;
import edu.ynu.e_platform.dto.ItemDTO;
import edu.ynu.e_platform.dto.OrderDTO;
import edu.ynu.e_platform.dto.ShopDTO;
import edu.ynu.e_platform.entities.Business;
import edu.ynu.e_platform.entities.Item;
import edu.ynu.e_platform.entities.Order;
import edu.ynu.e_platform.entities.Shop;
import edu.ynu.e_platform.service.BusinessService;
import edu.ynu.e_platform.service.ShopService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.security.SecurityUtil;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author manwen Yang
 */
@Slf4j
@Tag(name = "商家 实体控制器")
@RestController
@RequestMapping("/business")
public class BusinessController
        extends LogicController<BusinessService, BusinessDao, Business, Long> {

    @Resource
    ModelMapper modelMapper;

    @Resource
    ShopService shopSvc;

    public BusinessController(@Autowired BusinessService ls) {
        super(ls);
    }

    @Operation(summary = "注册商家")
    @GetMapping("/enroll")
    public Business enroll(String businessName, String password, Integer sex){

        return getService().enroll(businessName, password, sex);
    }

    @Operation(summary = "商家 新建商铺")
    @PostMapping("/createShop")
    public ShopDTO createShop(@RequestBody Shop shop){

        var result = shopSvc.POST(shop);
        return modelMapper.map(result, ShopDTO.class);
    }

    @Operation(summary = "商家 新建商铺（通过名字）")
    @GetMapping("/createShopByName")
    public ShopDTO createShopByName(Long BusinessId, String shopName){

        var result = getService().CreateShop(BusinessId, shopName);

        return modelMapper.map(result, ShopDTO.class);
    }

    @Operation(summary = "商家 创建商品")
    @GetMapping("/createItem")
    public ItemDTO createItem(Long businessId,
                              String itemName,
                              Integer type,
                              String description,
                              Double price){

        var result = getService().CreateItem(businessId, itemName, type, description, price);

        return modelMapper.map(result, ItemDTO.class);
    }

    @Operation(summary = "商家 查看所有门店")
    @GetMapping("/queryShops")
    public List<ShopDTO> queryShops(Long businessId){

        var results = getService().queryShops(businessId);

        return results.stream().map(
                shop -> modelMapper.map(shop, ShopDTO.class)
        ).collect(Collectors.toList());
    }

    @Operation(summary = "分页 按商家查询店铺")
    @GetMapping("/queryShopsPage")
    public List<ShopDTO> queryShopsPage(Integer pageStart, Integer pageSize, Long businessId){

        var results = getService().queryShopsPage(pageStart, pageSize, businessId);

        return results.stream().map(
                shop -> modelMapper.map(shop, ShopDTO.class)
        ).collect(Collectors.toList());
    }

    @Operation(summary = "商家 查看所有订单")
    @GetMapping("/queryOrders")
    public List<OrderDTO> queryOrders(Long shopId){
        var results = getService().queryOrders(shopId);

        return results.stream().map(
                order -> modelMapper.map(order, OrderDTO.class)
        ).collect(Collectors.toList());
    }

    @Operation(summary = "商家 确认订单")
    @GetMapping("/confirmOrder")
    public OrderDTO confirmOrder(Long businessId, Long orderId){

        var result = getService().confirmOrder(businessId, orderId);

        return modelMapper.map(result, OrderDTO.class);
    }

    @Operation(summary = "商家 取消订单")
    @GetMapping("/cancelOrder")
    public OrderDTO cancelOrder(Long businessId, Long orderId){

        var result = getService().confirmOrder(businessId, orderId);

        return modelMapper.map(result, OrderDTO.class);
    }

    @Operation(summary = "商家 查询所有商品")
    @GetMapping("/queryItems")
    public List<ItemDTO> queryItems(Long businessId){

        var results = getService().queryItems(businessId);

        return results.stream().map(
                item -> modelMapper.map(item, ItemDTO.class)
        ).collect(Collectors.toList());
    }

    @Operation(summary = "商家 分页查询所有商品")
    @GetMapping("/pageQueryItems")
    public List<ItemDTO> PageQueryItems(Integer pageStart, Integer pageSize, Long businessId){

        var results = getService().pageQueryItems(pageStart, pageSize, businessId);

        return results.stream().map(
                item -> modelMapper.map(item, ItemDTO.class)
        ).collect(Collectors.toList());
    }
}
